
<%@page import="multiface.web.Util"%>
<%@page import="multiface.kooper.jpa.*"%>
<%@page import="javax.persistence.*"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>

<%
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("KooperPU");
    EntityManager em = emf.createEntityManager();
    User us = (User) session.getAttribute("user");
    User u = em.find(User.class, us.getId());
    em.refresh(u);
%>
<html>
    <jsp:include flush="true" page="import.jsp"></jsp:include>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Post</title>
    </head>
    <body>
        <jsp:include flush="true" page="header.jsp"></jsp:include>
        <div id="container">
            <div id="wrapper">
                <jsp:include flush="true" page="left.jsp"></jsp:include>
                <div id="center" class="center">
                    <div class="follow image tiny">
                        <%
                            out.print("<p class=\"last-activity\"> Last Activity</p>");
                            for (Post p : u.getPostList()) {
                                out.print("<div class=\"post\">");
                                out.print("<div class=\"post-title\">" + p.getTitle() + "</div>");
                                out.print("<div class=\"post-text\">" + p.getText() + "</div>");
                                out.print("</div>");
                            }

                            int c = 0;
                            for (Question p : u.getQuestionList()) {
                                c++;
                                out.print("<div class=\"post\">");
                                out.print("<div class=\"post-title\">" + c + ". <a href=\"javascript:answer_of_post('" + p.getId() + "');\">" + p.getText() + "</a></div>");
                                out.print("<div class=\"post-subtitle\">" + Util.getData(p.getDate()) + "</div>");
                                for (Answer a : p.getAnswerList()) {
                                    String img = "";
                                    if (a.getUser().getImageTh() != null) {
                                        img = "<img class=\"image_tiny\" alt=\"image user\" src=\"../GetImageServlet?uid=" + a.getUser().getId() + "\"/> ";
                                    } else {
                                        img = "<img class=\"image_tiny\" alt=\"image user\" src=\"../engine/img/avatar.png\"/> ";
                                    }
                                    out.print("<div class=\"pt_" + p.getId() + " post-user hidden\">" + img + a.getUser().getName() + " " + a.getUser().getSurname() + " - " + Util.getData(a.getData()).toString() + "</div>");
                                    out.print("<div class=\"pt_" + p.getId() + " post-text hidden\">" + a.getText() + "</div>");

                                }
                        %>
                        <h2>Add Answer</h2>
                        <form method="post" action="../AddAnsweerServlet">
                            <textarea id="textarea" name="textarea"></textarea>
                            <input type="hidden" id="idquestion" name="idquestion" value="<%=p.getId()%>" />
                            <input type="submit" value="respond"/>
                        </form>
                        <%

                                out.print("</div>");
                            }

                        %>
                    </div>
                </div>
            </div>
        </div>
        <jsp:include flush="true" page="footer.jsp"></jsp:include>
    </body>
</html>
